package com.ruihu.admin.module.business.tasknode.controller;

import com.ruihu.admin.common.anno.OperateLog;
import com.ruihu.admin.common.domain.PageResultDTO;
import com.ruihu.admin.common.controller.BaseController;
import com.ruihu.admin.common.domain.ResponseDTO;
import com.ruihu.admin.common.domain.ValidateList;
import com.ruihu.admin.module.business.tasknode.constant.TaskNodeResponseCodeConst;
import com.ruihu.admin.module.business.tasknode.domain.dto.TaskNodeAddDTO;
import com.ruihu.admin.module.business.tasknode.domain.dto.TaskNodeUpdateDTO;
import com.ruihu.admin.module.business.tasknode.domain.dto.TaskNodeQueryDTO;
import com.ruihu.admin.module.business.tasknode.domain.vo.TaskNodeVO;
import com.ruihu.admin.module.business.tasknode.domain.vo.TaskNodeExcelVO;
import com.ruihu.admin.module.business.tasknode.service.TaskNodeService;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import org.apache.poi.ss.usermodel.Workbook;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * [ 任务环节 ]
 *
 * @author Tony
 * @version 1.0
 * @company 瑞虎（南京）信息技术有限公司
 * @copyright (c)  瑞虎（南京）信息技术有限公司Inc. All rights reserved.
 * @date 2022-08-30 17:07:35
 * @since JDK1.8
 */
@RestController
@Api(tags = {"任务环节"})
@OperateLog
public class TaskNodeController extends BaseController {

    @Autowired
    private TaskNodeService taskNodeService;

    @ApiOperation(value = "分页查询任务环节",notes = "@author Tony")
    @PostMapping("/taskNode/page/query")
    public ResponseDTO<PageResultDTO<TaskNodeVO>> queryByPage(@RequestBody TaskNodeQueryDTO queryDTO) {
        return taskNodeService.queryByPage(queryDTO);
    }

    @ApiOperation(value = "查询任务环节", notes = "查询任务环节")
    @GetMapping("/taskNode/listTaskNode")
    public ResponseDTO<List<TaskNodeVO>> queryTaskNodeList() {
        return taskNodeService.queryTaskNodeList();
    }

    @ApiOperation(value = "添加任务环节",notes = "@author Tony")
    @PostMapping("/taskNode/add")
    public ResponseDTO<String> add(@RequestBody @Validated TaskNodeAddDTO addTO){
        return taskNodeService.add(addTO);
    }

    @ApiOperation(value="修改任务环节",notes = "@author Tony")
    @PostMapping("/taskNode/update")
    public ResponseDTO<String> update(@RequestBody @Validated TaskNodeUpdateDTO updateDTO){
        return taskNodeService.update(updateDTO);
    }

    @ApiOperation(value="删除任务环节",notes = "@author Tony")
    @GetMapping("/taskNode/deleteById/{taskNodeId}")
    public ResponseDTO<String> deleteById(@PathVariable("taskNodeId") Long taskNodeId){
        if(taskNodeId==null){
            return ResponseDTO.wrap(TaskNodeResponseCodeConst.TASK_NODE_ID_NULL);
        }
        return taskNodeService.deleteById(taskNodeId);
    }

    @ApiOperation(value="批量删除任务环节",notes = "@author Tony")
    @PostMapping("/taskNode/deleteByIds")
    public ResponseDTO<String> delete(@RequestBody @Validated ValidateList<Long> idList) {
        return taskNodeService.deleteByIds(idList);
    }

    @ApiOperation(value = "批量导出", notes = "@author Tony")
    @PostMapping("/taskNode/export/batch")
    public void batchExport(@RequestBody @Validated ValidateList<Long> idList, HttpServletResponse response) {
        //查询数据
        List<TaskNodeExcelVO> taskNodeList = taskNodeService.queryBatchExportData(idList);
        //导出操作
        ExportParams ex = new ExportParams("任务环节", "Sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(ex, TaskNodeExcelVO.class, taskNodeList);
        downloadExcel("任务环节", workbook, response);
    }

    @ApiOperation(value = "导出全部", notes = "@author Tony")
    @PostMapping("/taskNode/export/all")
    public void exportAll(@RequestBody @Validated TaskNodeQueryDTO queryDTO, HttpServletResponse response) {
        //查询数据
        List<TaskNodeExcelVO> taskNodeList = taskNodeService.queryAllExportData(queryDTO);
        //导出操作
        ExportParams ex = new ExportParams("任务环节", "Sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(ex, TaskNodeExcelVO.class, taskNodeList);
        downloadExcel("任务环节", workbook, response);
    }

}
